//引入接口
import API from '../../config/server.js'
// const API=require('../../config/server.js')
Page({
  data: {
    originList: [],
    guangList: [],
    page: 1
  },
  onLoad() {
    //生命周期钩子
    this.guangData()
  },
  guangData() {
    let that = this
    wx.showLoading({
      title: '加载中'
    })
    wx.request({
      url: API.guangList + `?page=${that.data.page}`,
      method: 'GET',
      dataType: 'json',
      success: function(res) {
        var data = res.data.data
        // console.log(data.length==0)
        that.setData({
          guangList: data
        })
      },
      fail: function(res) {
        wx.showToast({
          title: '网络错误',
          icon: 'error',
        })
      },
      complete: function(res) {
        wx.hideLoading()
      },
    })
  },
  scrollToLower: function() { // 向底部滚动
    this.showLoading() // 调用显示loading提示框
    this.setData({
      originList: this.data.guangList
    })
    this.setData({
      page: this.data.page + 1
    })
    setTimeout(() => { // 做个延迟效果

      //无法利用原来封装的函数
      let that = this
      wx.showLoading({
        title: '加载中'
      })
      wx.request({
        url: API.guangList + `?page=${that.data.page}`,
        method: 'GET',
        dataType: 'json',
        success: function(res) {
          // console.log(res.data.current_page)
          // console.log(res.data.last_page)
          if (res.data.data.length == 0) {
            wx.showToast({
              title: '没有更多了',
              icon: 'none'
            })
            return
          }
          var data = res.data.data
          that.setData({
            guangList: that.data.originList.concat(data)
          })
        },
        fail: function(res) {
          wx.showToast({
            title: '网络错误',
            icon: 'error',
          })
        },
        complete: function(res) {
          wx.hideLoading()
        },
      })

      wx.hideLoading() // 数据加载完成之后隐藏loading提示框
    }, 1000)
  },
  showLoading: function() { // api交互，显示loading提示框
    wx.showLoading({
      title: '加载中',
    })
  },
  scrollToUpper: function() {
    wx.startPullDownRefresh() //通过方法调用刷新
    this.showLoading() // 调用显示loading提示框
    
    setTimeout(() => { // 做个延迟效果
      this.setData({ page: 1 })
      this.guangData()
      wx.hideLoading() // 数据加载完成之后隐藏loading提示框
      wx.stopPullDownRefresh()//结束刷新
    }, 1000)
  },
  toDetail(e) {
    let id = e.currentTarget.dataset.gid
    wx.navigateTo({
      url: `/pages/guangDetail/guangDetail?id=${id}`,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  }
})